<template>
  <div class="menu">
    <div
      v-for="(item, index) in props.chatMenu"
      :key="index"
      class="Box"
      :class="{ changeColor: currentIndex === index || roomId === item.userId }"
      @click="changeIndex(index, item)"
    >
      <div class="leftItem">
        <img :src="item.avatar" alt="头像" width="43" height="43" />
      </div>
      <div class="rightItem">
        <!-- <p>{{item.username}}</p> -->
        <p>{{ item.realname }}</p>
        <p>{{ item.message }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import Bus from "../bus";
import { defineProps, ref, defineEmits, reactive } from "vue";
import axios from "@/http";
import { chatMenuType } from "@/type/chat";
import $socket from "@/socket";
const emit = defineEmits(["seeCurrentUserMessage", "sendMessageToJoinRoom"]);
const props = defineProps({
  chatMenu: {
    type: Array as () => Array<chatMenuType>,
    default() {
      return [
        {
          userId: "number",
          username: "string",
          avatar: "string",
          realname: "string",
          message: "string",
        },
      ];
    },
  },
  roomId: {
    type: Number,
    default: null,
  },
});
const currentIndex = ref<number>(-1);
const lastUsername = ref<string>("root");
const initChat = reactive<Array<chatMenuType>>([]);
let chatItem = reactive<Array<chatMenuType>>([]); //存储我和她之间的对话

// initLeftMessage() {
//   this.$axios.get("/chat/lastSearch").then((res) => {
//     if (res.data.error === -1) {
//       this.initChat = res.data.data;

//       this.$emit("initChat",this.initChat)

//     }else{
//       this.initChat = [];
//     }
//   }).catch(err=>{
//     alert("服务器出现问题如多次出现请联系博主")
//   })
// },
const changeIndex = (index: number, item: chatMenuType) => {
  // $socket.emit("leave", {
  //   socketId: lastUsername,
  //   realname: "root",
  // });
  currentIndex.value = index;
  // $socket.emit("join", { socketId: item., realname: "root" });
  // //进入新房间，由于在新房间发送消息需要当前用户的username，因此改变自身username来进行谈话
  emit("sendMessageToJoinRoom", item.userId);
  // lastUsername.value = username; //保存当前房间的房间号，为了在更改房间时给上一个房间发送消息

  // // 然后开始从数据库里面提取我与她交流的信息
  // axios
  //   .get("/chat/search", {
  //     params: {
  //       username: "2628735401",
  //       Tousername: username,
  //     },
  //   })
  //   .then((res) => {
  //     if (res.data.error === -1) {
  //       chatItem = res.data.data;
  //       emit("seeCurrentUserMessage", chatItem);
  //     } else {
  //       // 错误提示
  //       Bus.$emit("live2dAboutSendError");
  //     }
  //   });
};
</script>

<style lang="scss" scoped>
.changeColor {
  background-color: #51a5f299 !important;
}
.menu {
  font-size: 17px;
  // display: inline-block;
  width: 230px;
  height: 100%;
  padding: 5px;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.4);
  overflow: auto;
  .Box {
    box-sizing: border-box;
    width: 100%;
    height: 55px;
    box-sizing: border-box;
    background-color: #ffffff;
    border-radius: 10px;
    margin: 5px 0;
    padding: 6px;
    display: flex;
    &:hover {
      background-color: rgb(217, 236, 255);
    }
    .leftItem {
      margin-right: 5px;
      img {
        width: 43px;
        height: 43px;
        border-radius: 43px;
      }
    }
    .rightItem {
      color: #333;
      font-size: 12px;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 3px 0;
      // text-overflow: ellipsis;
      // white-space: nowrap;
      p {
        font-weight: 200;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>
